import { MenuItem, BrowserWindow, dialog, Menu, ipcMain } from 'electron'

export class AppMenu {
    win: BrowserWindow

    constructor (win: BrowserWindow) {
      this.win = win
    }

    menuTemplate () {
      const template: any = [
        {
          label: '文件',
          submenu: [
            {
              label: '指定项目路径...',
              accelerator: 'CmdOrCtrl+P',
              click: this.assignRoute.bind(this)
            },
            {
              label: '修改数据路径...',
              accelerator: 'CmdOrCtrl+Q',
              id: 'configure-datapath',
              enabled: false
              // click: this.assignRoute.bind(this)
            },
            { type: 'separator' },
            {
              label: '转到schema',
              id: 'to-schema',
              accelerator: 'CmdOrCtrl+D'
              // click: this.assignRoute.bind(this)
            },
            {
              label: '回退',
              id: 'back',
              accelerator: 'CmdOrCtrl+B'
              // click: this.assignRoute.bind(this)
            },
            {
              label: '保存',
              id: 'save',
              accelerator: 'CmdOrCtrl+S'
              // click: this.assignRoute.bind(this)
            },
            { type: 'separator' },
            {
              label: '从资源管理器打开项目路径',
              id: 'open-projectpath'
              // click: this.assignRoute.bind(this)
            },
            {
              label: '从资源管理器打开数据路径',
              id: 'open-datapath'
              // click: this.assignRoute.bind(this)
            }
          ]
        },
        {
          label: '帮助',
          submenu: [{
            label: '文档',
            accelerator: 'CmdOrCtrl+F1'
            // click: this.assignRoute.bind(this)
          },
          { type: 'separator' },
          {
            label: 'Open Dev Tools',
            role: 'toggledevtools'
          }]
        }
      ]

      return template
    }

    createMenu (menuStrings = '', lang = ''): void {
      const menuTemplate = this.menuTemplate()
      const menu = Menu.buildFromTemplate(menuTemplate)
      Menu.setApplicationMenu(menu)

      // Add event listener for enabling/disabling menu items
      ipcMain.on('toggle-menu-items', (event, level: boolean, flag) => {
        // 第一层级：只要选了路径
        menu.getMenuItemById('configure-datapath').enabled = flag
        menu.getMenuItemById('open-projectpath').enabled = flag
        menu.getMenuItemById('open-datapath').enabled = flag
        // 第二层级：选择了文件
        menu.getMenuItemById('to-schema').enabled = flag
        // 第三层级：选择了文件且已修改
        menu.getMenuItemById('back').enabled = flag
        menu.getMenuItemById('save').enabled = flag
      })
    }

    assignRoute (menuItem: MenuItem | null = null, browserWindow: BrowserWindow | null = null, event: KeyboardEvent | null = null) {
      const res = dialog.showOpenDialogSync({
        title: '指定项目路径',
        buttonLabel: '读取',
        defaultPath: 'E:/UnityProjects/PVZ_Unity',
        filters: [
          { name: 'Custom File Type', extensions: ['js'] }
        ],
        properties: ['openDirectory']

      })
      if (res && res[0]) {
        console.log(res[0])
        const dataUrl = dialog.showOpenDialogSync({
          title: '读取数据配置路径',
          buttonLabel: '读取',
          defaultPath: res[0],
          filters: [
            { name: 'Custom File Type', extensions: ['js'] }
          ],
          properties: ['openDirectory']
        })
        if (this.win && dataUrl && dataUrl[0].startsWith(res[0])) {
          this.win.webContents.send('route-change', res[0], dataUrl[0])
        } else {
          dialog.showErrorBox('项目路径指定错误', '数据路径不在项目路径内')
        }
      }
    }
}
